package com.pureut.production.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.production.domain.AmHrGuard;
import com.pureut.production.domain.AmHrGuardskills;
import com.pureut.production.domain.AmHrSkills;
import com.pureut.production.domain.vo.AmHrGuardVo;
import com.pureut.production.domain.vo.AmHrGuardskillsRespVo;
import com.pureut.production.domain.vo.AmHrGuardskillsVo;
import com.pureut.production.service.AmHrGuardService;
import com.pureut.production.service.AmHrGuardskillsService;
import com.pureut.production.service.AmHrSkillsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/guardskills")
public class AmHrGuardSkillsController extends BaseController {
    @Resource
    private AmHrGuardskillsService amHrGuardskillsService;
    @Resource
    private AmHrGuardService amHrGuardService;
    @Resource
    private AmHrSkillsService amHrSkillsService;

    @GetMapping("/list")
    public TableDataInfo list(AmHrGuardskillsVo amHrGuardskillsVo) {
        startPage();
        AmHrGuardskills amHrGuardskills = new AmHrGuardskills();
        amHrGuardskills.setGuardCode(amHrGuardskillsVo.getGuardCode());
        amHrGuardskills.setSklCode(amHrGuardskillsVo.getSklCode());
        LambdaQueryWrapper<AmHrGuardskills> resultQuery = new LambdaQueryWrapper<AmHrGuardskills>();
        if(ObjectUtil.isNotNull(amHrGuardskillsVo.getSklCode())){
            resultQuery.like(AmHrGuardskills::getSklCode, amHrGuardskillsVo.getSklCode());
        }
        if(ObjectUtil.isNotNull(amHrGuardskillsVo.getGuardCode())){
            resultQuery.like(AmHrGuardskills::getGuardCode, amHrGuardskillsVo.getGuardCode());
        }
        List<AmHrGuardskills> list = amHrGuardskillsService.list(resultQuery);


        TableDataInfo dataTable = getDataTable(list);
        List<AmHrGuardskillsRespVo> collect = list.stream().map(item -> {
            AmHrGuardskillsRespVo amHrGuardskillsRespVo = new AmHrGuardskillsRespVo();
            amHrGuardskillsRespVo.setId(item.getId());
            amHrGuardskillsRespVo.setGuardCode(item.getGuardCode());
            amHrGuardskillsRespVo.setSklCode(item.getSklCode());
            amHrGuardskillsRespVo.setIsMust(item.getIsMust());
            amHrGuardskillsRespVo.setRemark(item.getRemark());

            AmHrGuard guardCode = amHrGuardService.getOne(
                    new QueryWrapper<AmHrGuard>()
                            .eq("guard_code", item.getGuardCode()));
            if (guardCode != null) {
                amHrGuardskillsRespVo.setGuardName(guardCode.getGuardName());
            }
            AmHrSkills sklCode = amHrSkillsService.getOne(new QueryWrapper<AmHrSkills>()
                    .eq("skl_code", item.getSklCode()));
            if (sklCode != null) {
                amHrGuardskillsRespVo.setSklName(sklCode.getSklName());
            }
            return amHrGuardskillsRespVo;
        }).collect(Collectors.toList());
        dataTable.setRows(collect);
        return dataTable;
    }

    @DeleteMapping("/delete/{guardskills}")
    public AjaxResult remove(@PathVariable("guardskills") Long id) {
        return toAjax(amHrGuardskillsService.removeById(id));
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody AmHrGuardskillsVo amHrGuardskillsVo) {
        AmHrGuardskills amHrGuardskills = new AmHrGuardskills();
        amHrGuardskills.setGuardCode(amHrGuardskillsVo.getGuardCode());
        amHrGuardskills.setSklCode(amHrGuardskillsVo.getSklCode());
        amHrGuardskills.setIsMust(amHrGuardskillsVo.getIsMust());
        amHrGuardskills.setRemark(amHrGuardskillsVo.getRemark());
        amHrGuardskills.setCreateTime(new Date());

        return toAjax(amHrGuardskillsService.save(amHrGuardskills));
    }

    @GetMapping("/view/{guardskills}")
    public AjaxResult view(@PathVariable("guardskills") Long id) {
        return AjaxResult.success(amHrGuardskillsService.getById(id));
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody AmHrGuardskills amHrGuardskills) {
        return toAjax(amHrGuardskillsService.updateById(amHrGuardskills));
    }
}
